package com.tuzhi.test.sys.web.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.tuzhi.base.jpa.domain.WhereConditionBO;
import com.tuzhi.base.jpa.domain.WhereStatementBO;
import com.tuzhi.base.jpa.enums.AndOrOperate;
import com.tuzhi.base.jpa.enums.QueryTypeEnum;
import com.tuzhi.base.jpa.healper.WhereConditionHelper;
import com.tuzhi.base.service.BaseService;
import com.tuzhi.base.web.controller.AbsBaseController;
import com.tuzhi.base.web.helper.AjaxResultHelper;
import com.tuzhi.base.web.vo.AjaxResultVO;
import com.tuzhi.base.web.vo.PageVO;
import com.tuzhi.common.constants.ResultConstant;
import com.tuzhi.test.sys.jpa.entity.SysRoleEntity;
import com.tuzhi.test.sys.jpa.entity.SysUserEntity;
import com.tuzhi.test.sys.service.SysRoleService;
import com.tuzhi.test.sys.service.SysUserService;
import com.tuzhi.util.DateTzUtil;
import com.tuzhi.util.ObjectTzUtil;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@CrossOrigin
@RequestMapping("/sys/user")
public class SysUserController extends AbsBaseController<SysUserEntity,Long> {

    @Autowired
    SysUserService service;

    @Autowired
    SysRoleService roleService;

    static WhereStatementBO sysUserWhereStatementBO = new WhereStatementBO();

    static {
        //查询条件申明
        //passNull=true设置，对空值是否不带入条件查询，默认true空不带入查询
        //Date.class设置，指定传入数据最后转为什么对象去数据库查询
//        sysUserWhereStatementBO.addQuery(
//                "username", QueryTypeEnum.like, AndOrOperate.and, false);
        sysUserWhereStatementBO.addQuery("username", QueryTypeEnum.like, AndOrOperate.and);
        sysUserWhereStatementBO.addQuery("nickname");
        sysUserWhereStatementBO.addQuery("createDate",
                        QueryTypeEnum.between, AndOrOperate.and, true, Date.class);

        //添加排序的申明
        sysUserWhereStatementBO.addSort("username");
        sysUserWhereStatementBO.addSort("sex");

    }

    @Override
    protected BaseService getThisService() {
        return service;
    }

    @GetMapping(value = { "/find/{id}"})
    public SysUserEntity find(@PathVariable(value = "id", required = true) Long id) {
        SysUserEntity entity =  service.findById(id);
        return entity;
    }

    @GetMapping(value = { "/testAdd/{id}"})
    public SysUserEntity testAdd(@PathVariable(value = "id", required = true) Long id) {
        SysUserEntity entity =  service.findById(id);
        SysRoleEntity roleEntity = new SysRoleEntity();
        roleEntity.setName("test" + RandomUtils.nextInt());
        roleEntity.setStatus(0);
        roleEntity.setTitle("测试角色");
        entity.getRoles().add(roleEntity);
        service.save(entity);
        return entity;
    }

    @GetMapping(value = { "/test/putRoles"})
    public AjaxResultVO putRoles(@RequestBody String json) {
        JSONObject jsonObject = JSON.parseObject(json);
        Long userId = jsonObject.getLong("userId");
        List<Long> list = jsonObject.getList("roleIds", Long.class);
        if (ObjectTzUtil.existNull(userId,list)) {
            return AjaxResultHelper.build(ResultConstant.ERR_PARAM_NULL);
        }
        service.putRoles(userId, list);
        return AjaxResultHelper.success();
    }

    @GetMapping(value = { "/test/moveOutRoles"})
    public AjaxResultVO moveOutRoles(@RequestBody String json) {
        JSONObject jsonObject = JSON.parseObject(json);
        Long userId = jsonObject.getLong("userId");
        List<Long> list = jsonObject.getList("roleIds", Long.class);
        if (ObjectTzUtil.existNull(userId, list)) {
            return AjaxResultHelper.build(ResultConstant.ERR_PARAM_NULL);
        }
        service.moveOutRoles(userId, list);
        return AjaxResultHelper.success();
    }

    @GetMapping(value = { "/test/findPager"})
    public AjaxResultVO findPager(@RequestBody PageVO pageVO) {

        /**
         *{
         *   "page": 1,
         *   "reqData": {
         *     "username": "a",
         *     "createDate": [
         *       "2003-01-1 00:00:00",
         *       "2023-04-26 00:00:00"
         *     ],
         *     "sortCols": [
         *       {
         *         "username": "ace"
         *       },
         *       {
         *         "sex": "ace"
         *       }
         *     ]
         *   }
         * }
         */


        //基于申明规则自动组装分页查询条件
        pageVO = findPager(pageVO, sysUserWhereStatementBO);
        return AjaxResultHelper.success().ofData(pageVO);
    }

    @GetMapping(value = { "/test/findPager2"})
    public AjaxResultVO findPager2(@RequestBody PageVO pageVO) {

        /**
         * {"reqData":{
         *     "username": "zhf"
         * }}
         */

        //基于申明规则自动组装分页查询条件，再另外扩展查询条件

        if (pageVO == null) {
            pageVO = new PageVO();
        }
        Map map = (Map) pageVO.getReqData();
        WhereConditionBO whereConditionBO = WhereConditionHelper.trans(sysUserWhereStatementBO, map);
        whereConditionBO.addQueryConditionBO("password", "123");
        pageVO = findPager(pageVO, whereConditionBO);
        return AjaxResultHelper.success().ofData(pageVO);
    }

    @GetMapping(value = { "/test/findOne2/{val}"})
    public AjaxResultVO findOne2(@PathVariable String val) {

        //自定义单个查询

        //111111111
//        WhereConditionBO whereConditionBO = WhereConditionHelper.build();
//        whereConditionBO.addQueryConditionBO4List("username", val);


        //2222222222222222
        WhereConditionBO whereConditionBO = WhereConditionHelper.build("username", val);
        whereConditionBO.addQueryConditionBO("nickname", "a", QueryTypeEnum.like, AndOrOperate.and);
        List<Date> list = new ArrayList<>();
        list.add(DateTzUtil.parseDate("2022-01-01"));
        list.add(DateTzUtil.parseDate("2024-01-01"));
        whereConditionBO.addQueryConditionBO4List("createDate", list, QueryTypeEnum.between, AndOrOperate.and);
        SysUserEntity sysUserEntity = findOne(whereConditionBO);
        return AjaxResultHelper.success().ofData(sysUserEntity);
    }

}
